package com.xuchaotong.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSON;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

/**
 * @author EASON.TONG
 * @create 2021/03/21
 **/
public class TimeSplitTest {

    @Test
    public void testGetStartTime() {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(time);

        System.out.println(LocalDateTimeUtil.format(beginOfDay, DatePattern.NORM_DATETIME_PATTERN));
    }

    @Test
    public void testGetStartEndTime() {
        Date preTime = DateUtil.yesterday();

        Date beginOfDateTime = DateUtil.beginOfDay(preTime);
        Date endOfDateTime = DateUtil.offset(beginOfDateTime, DateField.DAY_OF_MONTH, 1);

        String startTime = DateUtil.formatDateTime(beginOfDateTime);
        String endTime = DateUtil.formatDateTime(endOfDateTime);

        System.out.println(startTime + "---->" + endTime);
    }

    @Test
    public void testFormat(){
        Date time = DateUtil.parseDateTime("2021-03-21 06:00:00");

        System.out.println(DateUtil.format(time,"HH:mm"));
    }


    @Test
    public void testSplit() {
        System.out.println(JSON.toJSON(TimeSplitTest.getSplitTimeList()));
    }

    private static List<Map<String, String>> getSplitTimeList(ChronoUnit chronoUnit) {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime preTime = LocalDateTimeUtil.offset(time, -1, ChronoUnit.DAYS);
        LocalDateTime beginOfDateTime = LocalDateTimeUtil.beginOfDay(preTime);
        LocalDateTime endOfDateTime = LocalDateTimeUtil.endOfDay(preTime);

        long interval = LocalDateTimeUtil.between(beginOfDateTime, endOfDateTime, chronoUnit);

        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> statisticsTime = null;
        LocalDateTime startTime;
        LocalDateTime endTime;
        for (int i = 0; i <= interval; i++) {
            statisticsTime = new HashMap<>(2);
            startTime = LocalDateTimeUtil.offset(beginOfDateTime, i, chronoUnit);
            endTime = LocalDateTimeUtil.offset(startTime, 1, chronoUnit);

            statisticsTime.put("startTime",
                LocalDateTimeUtil.format(startTime, DatePattern.NORM_DATETIME_PATTERN));
            statisticsTime.put("endTime",
                LocalDateTimeUtil.format(endTime, DatePattern.NORM_DATETIME_PATTERN));
            list.add(statisticsTime);
        }

        return list;
    }

    private static List<Map<String, String>> getSplitTimeList() {
        Date preTime = DateUtil.yesterday();

        Date beginOfDateTime = DateUtil.beginOfDay(preTime);
        Date endOfDateTime = DateUtil.offset(beginOfDateTime, DateField.DAY_OF_MONTH, 1);

        long interval = DateUtil.between(beginOfDateTime, endOfDateTime, DateUnit.HOUR);
        System.out.println(interval);

        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> statisticsTime = null;
        Date startTime;
        Date endTime;
        for (int i = 0; i < interval; i++) {
            statisticsTime = new HashMap<>(3);
            startTime = DateUtil.offset(beginOfDateTime, DateField.HOUR_OF_DAY, i);
            endTime = DateUtil.offset(startTime, DateField.HOUR_OF_DAY, 1);

            statisticsTime.put("startTime", DateUtil.formatDateTime(startTime));
            statisticsTime.put("endTime", DateUtil.formatDateTime(endTime));
            statisticsTime.put("intervalType", "HOUR");
            list.add(statisticsTime);
        }
        statisticsTime = new HashMap<>(2);
        statisticsTime.put("startTime", DateUtil.formatDateTime(beginOfDateTime));
        statisticsTime.put("endTime", DateUtil.formatDateTime(endOfDateTime));
        statisticsTime.put("intervalType", "DAY");
        list.add(statisticsTime);

        return list;
    }

}
